import { Role } from '@/types/role';
import { pageReq } from '@/common/types/report/report';
import apiClient from '../apiClient';


export interface LimitsFetchReq {
  keywords?: string;
  page?: number;
  page_size?: number;
  user_name?: string;
  action_type?: string;
  start_time?: string | null;
  end_time?: string | null;

}


export interface AddRoleReq {
  name: string;
  description: string;
  parent_id?: string | undefined;
}

export enum LimitsApi {
  LimitsUrl = '/openapi/v1/user_action_log/user_action_list',
  LimitsActionType = '/openapi/v1/user_action_log/action_type_available',



  // SignIn = '/auth/signin',
  SignIn = '/openapi/v1/user/loginByPassword',
  // SignIn = '/ruigpt/help/user/account/loginByPassword',
  // AuthCx = '/openapi/v1/userL/cx/sso/login',
  AuthCx = '/openapi/v1/user/cx/sso/login_create',
  SignUp = '/auth/signup',
  Logout = '/auth/logout',
  Refresh = '/auth/refresh',
  Permissions = '/openapi/v1/permissions',
  Role = '/openapi/v1/role',
  FetchRoleApi = '/openapi/v1/roles',
  UpdateRoleDebug = '/openapi/v1/debug/switch_role',
  // RolePermissions = '/openapi/v1/role_permissions',
  RolePermissions = '/openapi/v1/role',

  LicenseApi = '/agent/api/v1/license',
  LicenseRedeemApi = '/agent/api/v1/redeem_license_url',
  // 用户管理
  RuigptWorkApi = '/openapi/v1/user'
}

//行为日志列表
const limitsList = (data: LimitsFetchReq) => apiClient.post({ url: LimitsApi.LimitsUrl, data });
//获取类型列表
const limitsActionType = () => apiClient.get({ url: LimitsApi.LimitsActionType });


const permissions = () => apiClient.get({ url: LimitsApi.Permissions });
// const updatePermissions = () => apiClient.put({ url: LimitsApi.Permissions });

const addRole = (data: AddRoleReq) => apiClient.post<Role>({ url: LimitsApi.Role, data });
const deleteRole = (id: string) => apiClient.delete({ url: LimitsApi.Role + `/${id}` });

// 获取角色列表，包括conditions和permissions
const getRole = () => apiClient.get<Role[]>({ url: LimitsApi.Role });

// const getRoles = () => apiClient.get({ url: LimitsApi.UpdateRole});
const getRoles = () => apiClient.get({ url: LimitsApi.FetchRoleApi + '?applicationId=999' });



// const getRolePermissions = () => apiClient.get({ url: LimitsApi.RolePermissions });
const getRolePermissions = (role_id = '') => {
  let url: string = LimitsApi.RolePermissions;
  if (role_id) {
    url += `?role_id=${role_id}`;
  }
  return apiClient.get({ url });
};


// License 导入和信息获取
const fetchLicense = () => apiClient.get({ url: LimitsApi.LicenseApi })
const fetchLicenseUrl = () => apiClient.get({ url: LimitsApi.LicenseRedeemApi })


// 用户管理
const fetchUser = (data: LimitsFetchReq) => apiClient.get({ url: LimitsApi.RuigptWorkApi, params: data })
const deleteUser = (id: string) => apiClient.delete({ url: LimitsApi.RuigptWorkApi + `/${id}` })

const updateRoleType = (id: string) => apiClient.post({ url: LimitsApi.RolePermissions + `/${id}` + '/subadmin' })

export default {
  limitsList,
  limitsActionType,



  permissions,
  deleteRole,
  addRole,
  getRole,
  getRoles,
  getRolePermissions,

  fetchUser,
  deleteUser,

  fetchLicense,
  fetchLicenseUrl,
  updateRoleType
};
